package com.tipfy.tmx;


class TiledImage
{
    /**
     * nonNegativeInteger
     */
    public var id:Int;

    /**
     * formatT (only "png" allowed)
     */
    public var format:String;

    /**
     * anyURI
     */
    public var source:String;

    /**
     * hexColourT
     */
    public var trans:String;

    /**
     * base64Binary
     */
    public var data:String;

    /**
     * Constructor.
     */
    public function new(xml:Xml)
    {
        // XXX
        var validFormat:Array<String> = this._getValidFormat();

        // Attributes.
        this.id = TiledAttributes.getNonNegativeInt(xml, "id", null, false);
        this.format = TiledAttributes.getEnum(xml, "format", validFormat,
            "png");
        // AnyUri
        this.source = TiledAttributes.get(xml, "source", null, false);
        // TODO: validate hexColourT: [a-fA-F0-9]{6}
        this.trans = TiledAttributes.get(xml, "trans", null, false);

        // Child elements.
        for (elem in xml.elements())
        {
            switch (elem.nodeName)
            {
                case "data":
                    this._loadData(elem);
                default:
                    throw "Invalid element <" + elem.nodeName +
                        "> in <image>";
            }
        }
    }

    /**
     * Which other way to validate possible enum values?
     */
    private inline function _getValidFormat():Array<String>
    {
        var res:Array<String> = new Array<String>();
        res.push("png");
        return res;
    }

    /**
     * <data>. minOccurs="0", maxOccurs="1"
     */
    private function _loadData(xml:Xml):Void
    {
        if (this.data != null)
        {
            throw "<data> is already defined in <image>.";
        }

        // TODO
        // - content value
        // - encoding attribute required ("base64")
    }
}
